/*
 * Copyright (c) 2023 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.executor;

import cn.xphsc.kubernetes.core.query.NamespaceQuery;
import cn.xphsc.kubernetes.core.query.StorageQuery;
import io.kubernetes.client.PodLogs;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.*;
import org.apache.commons.lang3.StringUtils;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description: 用于获取Kubernetes中的所有 Stotage Executor
 * @since 1.0.0
 */
public class StotageApiExecutor<T> extends AbstractExecutor<T> {
    private String stotageTypeName;
    private String stotageName;
    private  T entity;
    private StorageV1Api storageV1Api;
    private  StorageQuery storageQuery;
    private NamespaceQuery namespaceQuery;
    public StotageApiExecutor(ApiClient client,StorageQuery storageQuery, StotageTypeMode stotageTypeMode, StotageMode stotageMode) {
        super(client);
        this.stotageTypeName=stotageTypeMode.name();
        this.stotageName=stotageMode.name();
        storageV1Api=new StorageV1Api(this.getClient());
        this.storageQuery=storageQuery;
    }
    public StotageApiExecutor(ApiClient client, NamespaceQuery namespaceQuery, StotageTypeMode stotageTypeMode, StotageMode stotageMode) {
        super(client);
        this.stotageTypeName=stotageTypeMode.name();
        this.stotageName=stotageMode.name();
        storageV1Api=new StorageV1Api(this.getClient());
        this.namespaceQuery=namespaceQuery;
    }

    @Override
    protected  T doExecute() {
        if (StringUtils.isNotBlank(stotageName)&&StringUtils.isNotBlank(stotageTypeName)) {
            switch (stotageTypeName){
                case "CREATE":
                    break;
                case "LIST":
                    switch (stotageName){
                        case "DF":
                            try {
                                entity= (T) storageV1Api.listStorageClass(storageQuery.pretty(), storageQuery.allowWatchBookmarks(), storageQuery._continue(), storageQuery.fieldSelector(), storageQuery.labelSelector(), storageQuery.limit(), storageQuery.resourceVersion(), storageQuery.resourceVersionMatch(), storageQuery.timeoutSeconds(), storageQuery.watch());
                            } catch (ApiException e) {
                                throw new RuntimeException(e);
                            }
                            break;
                        case "HTTPINFO":
                            try {
                                entity= (T) storageV1Api.listStorageClassWithHttpInfo(storageQuery.pretty(), storageQuery.allowWatchBookmarks(), storageQuery._continue(), storageQuery.fieldSelector(), storageQuery.labelSelector(), storageQuery.limit(), storageQuery.resourceVersion(), storageQuery.resourceVersionMatch(), storageQuery.timeoutSeconds(), storageQuery.watch());
                            } catch (ApiException e) {
                                throw new RuntimeException(e);
                            }
                            break;
                        case "CSISC":
                            try {
                                entity= (T) storageV1Api.listNamespacedCSIStorageCapacity(namespaceQuery.namespace(),namespaceQuery.pretty(), namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                            } catch (ApiException e) {
                                throw new RuntimeException(e);
                            }
                            break;
                        case "CSISAllNS":
                            try {
                                entity= (T) storageV1Api.listCSIStorageCapacityForAllNamespaces( namespaceQuery.allowWatchBookmarks(), namespaceQuery._continue(), namespaceQuery.fieldSelector(), namespaceQuery.labelSelector(), namespaceQuery.limit(),namespaceQuery.pretty(), namespaceQuery.resourceVersion(), namespaceQuery.resourceVersionMatch(), namespaceQuery.timeoutSeconds(), namespaceQuery.watch());
                            } catch (ApiException e) {
                                throw new RuntimeException(e);
                            }
                            break;
                        default:
                    }
                    break;

                case "PATCH":
                    break;
                case "READ":
                    break;
                case "REPLACE":
                    break;
                default:

            }
        }
        return (T) entity;
    }
    public static enum StotageTypeMode {
        /**
         *
         */
        CREATE,
        LIST,
        PATCH,
        READ,
        REPLACE;
        private StotageTypeMode() {

        }
        private StotageTypeMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static enum StotageMode {

        /**
         *
         */
        DF,
        HTTPINFO,
        CSISC,
        CSISAllNS,
        AUTO;

        private StotageMode() {

        }
        private StotageMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


